package view.CURD;

import library.controller.FrameListener;
import library.controller.JTextFieldListener;
import library.controller.LoginListener;
import library.po.User;
import library.service.IdentityService;
import library.service.impl.IdentityServiceImpl;
import view.login.LoginDialog;
import view.main.MainFrame;

import javax.swing.*;
import java.awt.*;

/**
 * 添加用户信息对话框
 * @author 27261
 *
 */
public class UserDialog extends JDialog {
    private static final long serialVersionUID = 1L;
    private LoginListener loginListener;
    private FrameListener frameListener;
    private IdentityService identityService = new IdentityServiceImpl();
    private User user;
    private JTextField userIdTextField = new JTextField(20);
    private JTextField userNameTextField = new JTextField(20);
    private JPasswordField passwordField = new JPasswordField(20);
    private JTextField creditField = new JTextField(20);
    private JLabel idLabel = new JLabel("用户名:");
    private JLabel passwordLabel = new JLabel("密 码");
    private JLabel nameLabel = new JLabel("姓 名:");
    private JLabel creditLabel = new JLabel("信 用:");
    private JLabel sexLabel = new JLabel("性 别:");
    private JLabel typeLabel = new JLabel("身 份:");
    private JComboBox sexBox = new JComboBox();
    private JComboBox typeBox = new JComboBox();
    private JButton okButton = new JButton("确认");
    private JPanel buttonBar = new JPanel();
    private JPanel userPanel = new JPanel();
    public UserDialog(OverviewUserDialog overviewUserDialog,String title,User user){
        this.setTitle(title);
        this.user = user;
        frameListener = overviewUserDialog.getListener();
        frameListener.setUserAddDialog(this);
        this.setResizable(false);
        this.add(getUserPanel());
        this.setModal(true);
        this.setSize(250, 390);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.validate();
        this.closeCurrentDialog();
    }
    public UserDialog(LoginDialog owner, String title, User user) {
        this.loginListener = new LoginListener();
        this.loginListener.setUserAddDialog(this);
        this.setTitle(title);
        this.setResizable(false);
        this.user = user;
        this.add(getUserPanel());
        this.setModal(true);
        this.setSize(250, 390);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.validate();
        this.closeCurrentDialog();
    }
    public UserDialog(MainFrame owner, String title, User user) {
        this.frameListener = owner.getMainListener();  //让MainFrame和Dialog共用同一个controller对象
        this.frameListener.setUserDialog(this);
        this.setTitle(title);
        this.setResizable(false);
        this.user = user;
        this.add(getUserPanel());
        this.setModal(true);
        this.setSize(250, 390);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.validate();
        this.closeCurrentDialog();
    }
    public void closeCurrentDialog() {
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    }
    /**
     * 添加学生信息表单面板
     * @return
     */
    private JPanel getUserPanel() {
        userPanel = new JPanel();
        if("注册".equals(this.getTitle())) {
            userIdTextField.addFocusListener(new JTextFieldListener(userIdTextField, "用户名由20位以下字母和数字组成"));
            passwordField.addFocusListener(new JTextFieldListener(passwordField, "密码由20位以下字母和数字组成"));
            userNameTextField.addFocusListener(new JTextFieldListener(userNameTextField, "您的姓名"));
            creditField.setText("1");
            creditField.setEditable(false);
            okButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
            okButton.setPreferredSize(new Dimension(100, 30));
            okButton.setActionCommand("userAddPost"); //添加学生信息
            sexBox.addItem("--请选择--");
            sexBox.addItem("男");
            sexBox.addItem("女");
            typeBox.addItem("--请选择--");
            typeBox.addItem("借阅者");
            typeBox.addItem("系统管理员&借阅者");
            typeBox.addItem("图书管理员&借阅者");
            typeBox.addItem("系统管理员&图书管理员&借阅者");
        }else if ("添加用户".equals(this.getTitle())){
                userIdTextField.addFocusListener(new JTextFieldListener(userIdTextField, "用户名由20位以下字母和数字组成"));
                passwordField.addFocusListener(new JTextFieldListener(passwordField, "密码由20位以下字母和数字组成"));
                userNameTextField.addFocusListener(new JTextFieldListener(userNameTextField, "您的姓名"));
                creditField.setText("1");
                okButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
                okButton.setPreferredSize(new Dimension(100, 30));
                okButton.setActionCommand("UserAddPost"); //添加学生信息
                okButton.addActionListener(frameListener);
                sexBox.addItem("--请选择--");
                sexBox.addItem("男");
                sexBox.addItem("女");
                typeBox.addItem("--请选择--");
                typeBox.addItem("借阅者");
                typeBox.addItem("系统管理员&借阅者");
                typeBox.addItem("图书管理员&借阅者");
                typeBox.addItem("系统管理员&图书管理员&借阅者");
        }
        else if ("用户修改信息".equals(this.getTitle())){
            sexBox.addItem("--请选择--");
            sexBox.addItem("男");
            sexBox.addItem("女");
            userIdTextField.setText(user.getUserId());
            userIdTextField.setEditable(false);
            passwordField.setText(user.getUserPassword());
            userNameTextField.setText(user.getUserName());
            typeBox.addItem((new LoginListener().typeToIdentity(user.getUserType())));
            typeBox.setEditable(false);
            creditField.setText(user.getUserCredit().toString());
            creditField.setEditable(false);
            okButton = new JButton("确定修改");
            okButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
            okButton.setPreferredSize(new Dimension(100, 30));
            okButton.setActionCommand("userUpdatePost"); //修改学生信息
            okButton.addActionListener(frameListener);
        }
        else if ("修改用户信息".equals(this.getTitle())){
            sexBox.addItem("--请选择--");
            sexBox.addItem("男");
            sexBox.addItem("女");
            userIdTextField.setText(user.getUserId());
            passwordField.setText(user.getUserPassword());
            userNameTextField.setText(user.getUserName());
            creditField.setText(user.getUserCredit().toString());
            typeBox.addItem((new LoginListener().typeToIdentity(user.getUserType())));
            if(!"借阅者".equals(identityService.selectByType(user.getUserType()).getIdentityName())){
                typeBox.addItem("借阅者");
            }
            if(!"系统管理员&借阅者".equals(identityService.selectByType(user.getUserType()).getIdentityName())){
                typeBox.addItem("系统管理员&借阅者");
            }
            if(!"图书管理员&借阅者".equals(identityService.selectByType(user.getUserType()).getIdentityName())){
                typeBox.addItem("图书管理员&借阅者");
            }
            if(!"系统管理员&图书管理员&借阅者".equals(identityService.selectByType(user.getUserType()).getIdentityName())){
                typeBox.addItem("系统管理员&图书管理员&借阅者");
            }
            okButton = new JButton("确定修改");
            okButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
            okButton.setPreferredSize(new Dimension(100, 30));
            okButton.setActionCommand("UpdateUser"); //修改学生信息
            okButton.addActionListener(frameListener);
        }
        okButton.addActionListener(loginListener);
        idLabel.setFont(new Font("微软雅黑",Font.PLAIN,14));
        passwordLabel.setFont(new Font("微软雅黑",Font.PLAIN,14));
        nameLabel.setFont(new Font("微软雅黑",Font.PLAIN,14));
        sexLabel.setFont(new Font("微软雅黑",Font.PLAIN,14));
        typeLabel.setFont(new Font("微软雅黑",Font.PLAIN,14));
        creditLabel.setFont(new Font("微软雅黑",Font.PLAIN,14));
        userPanel.add(idLabel);
        userPanel.add(userIdTextField);
        userPanel.add(passwordLabel);
        userPanel.add(passwordField);
        userPanel.add(nameLabel);
        userPanel.add(userNameTextField);
        userPanel.add(creditLabel);
        userPanel.add(creditField);
        userPanel.add(sexLabel);
        userPanel.add(sexBox);
        userPanel.add(typeLabel);
        userPanel.add(typeBox);
        userPanel.add(okButton);
        return userPanel;
    }
    public JTextField getUserIdTextField(){
        return this.userIdTextField;
    }
    public JTextField getUserNameTextField(){
        return this.userNameTextField;
    }
    public JPasswordField getPasswordField(){
        return this.passwordField;
    }
    public JComboBox getSexBox(){
        return this.sexBox;
    }
    public JComboBox getTypeBox(){
        return this.typeBox;
    }
    public User getUserInstance(){
        User user = new User();
        user.setUserId(this.userIdTextField.getText().trim());
        String nameInput = userNameTextField.getText().trim();
        if(nameInput != null && !"".equals(nameInput) && !"您的姓名".equals(nameInput)) {
            user.setUserName(nameInput);
        }else {
            JOptionPane.showMessageDialog(this, "姓名不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
        }
        String passwordInput = passwordField.getText().trim();
        if(passwordInput != null && !"".equals(passwordInput)) {
            user.setUserPassword(passwordInput);
        }
        else {
            JOptionPane.showMessageDialog(this, "密码不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
        }
        String sexInput = sexBox.getSelectedItem().toString().trim();
        if(sexInput != null && !"".equals(sexInput)&& !sexInput.equals("--请选择--")) {
            user.setUserSex(sexInput);
        }
        else
            JOptionPane.showMessageDialog(this, "请选择您的性别","输入错误",JOptionPane.ERROR_MESSAGE);
        String typeInput = typeBox.getSelectedItem().toString().trim();
        if(typeInput != null && !"".equals(typeInput)&& !typeInput.equals("--请选择--")) {
            user.setUserType(frameListener.identityToType(typeInput));
        }
        else
            JOptionPane.showMessageDialog(this, "请选择该用户的身份","输入错误",JOptionPane.ERROR_MESSAGE);
        Integer creditInput = Integer.parseInt(creditField.getText().trim());
        if (creditInput != null&&!"".equals(creditInput)){
            user.setUserCredit(creditInput);
        }
        else
            JOptionPane.showMessageDialog(this, "信用为空！","输入错误",JOptionPane.ERROR_MESSAGE);
        return user;
    }

}
